/*
#Copyright (c) 2013, Philippe Bordron <philippe.bordron@gmail.com>
#
# This file is part of SIPPER.
#
# DCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU LESSER GENERAL PUBLIC LICENSE for more details.
#
# You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
# along with SIPPER.  If not, see <http://www.gnu.org/licenses/>
*/
package combi.wip.sipper.exec;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import combi.sipper.io.IOAssociation;
import combi.sipper.io.IOQueriesFile;
import combi.utils.Couple;

public class GenerateQueriesForSipper {

	/**
	 * Create a file that contains the set of all requested k-SIPs.
	 * Constraints about requested k-SIPs must take place in parameters.
	 * In this version, the cartesien product between elements in the entry files is computed 
	 */

	public static void main (String[] args)
	{
		String inFile = args[0];
		String assoFile = args[1];
		String outFile = args[2];
		try {
			IOQueriesFile reader = new IOQueriesFile(inFile);
			List<Couple<List<String>, List<String>>> listOfQueriesByGroups = reader.read();
			//System.err.println(listOfQueriesByGroups);
			Map<String, Set<String>> mapOfAssociations = IOAssociation.loadString(assoFile, "(?:([^\\t]+)\\t(.+))");
			IOQueriesFile output = new IOQueriesFile(outFile);
			output.write(generateListOfQueryByVertices(listOfQueriesByGroups,mapOfAssociations));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static List<Couple<List<String>, List<String>>> generateListOfQueryByVertices(
			List<Couple<List<String>, List<String>>> listOfQueriesByGroups,
			Map<String, Set<String>> mapOfAssociations)
	{
		List<Couple<List<String>, List<String>>> result = new ArrayList<Couple<List<String>, List<String>>>(listOfQueriesByGroups.size());
		for(Couple<List<String>, List<String>> c : listOfQueriesByGroups)
		{
			Couple<List<String>, List<String>> newCouple = new Couple<List<String>, List<String>>(new ArrayList<String>(), new ArrayList<String>());
			for (String l : c.getFirst())
			{
				//System.err.println(l+ ": " + mapOfAssociations.get(l));
				newCouple.getFirst().addAll(mapOfAssociations.get(l));
			}
			for (String l : c.getSecond())
			{
				//System.err.println(l+ ": " + mapOfAssociations.get(l));
				newCouple.getSecond().addAll(mapOfAssociations.get(l));
			}
			result.add(newCouple);
		}
		return result;
	}



}
